package com.internship.controller;

import java.sql.PseudoColumnUsage;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.internship.model.DepartmentInfo;
import com.internship.model.Plan;
import com.internship.page.Page;
import com.internship.service.Const;
import com.internship.service.DepartmentInfoService;
import com.internship.service.PlanService;


@Controller
@RequestMapping("plan")
public class PlanController {
	private final Logger logger = Logger.getLogger(PlanController.class);
	
	@Autowired
	private PlanService planService;
	@Autowired
	private DepartmentInfoService 	departmentInfoService;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}
	
	@RequestMapping("/plan_list.do")
	public ModelAndView getPlanList() {
		return getPlanList(null,1);
	}
	
	private List<DepartmentInfo>  departmentInfoList = null;
	@RequestMapping("/plan_page_list.do")
	public ModelAndView getPlanList(@RequestParam String DeptCode,@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("plan");
		HttpSession session = getSession();
		try {
			if (String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("2")||String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("1")) {
				Page<Plan> pageList = planService.getPlanList(DeptCode, pageNumber);
				this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
				mv.addObject("currentPage", pageNumber);
				mv.addObject("DeptCode", DeptCode);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("departmentInfoList",departmentInfoList);
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("planList", pageList.getResultList());
			}else {
				Page<Plan> pageList = planService.getPlanList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)), pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("planList", pageList.getResultList());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mv;
	}
	/**?
	 * 添加实习计划
	 * @return
	 */
	@RequestMapping("/add_plan.do")
	public ModelAndView addPlan() {
		ModelAndView mv = new ModelAndView("plan_add");
		this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
		mv.addObject("departmentInfoList",departmentInfoList);
		return mv;
	}
	@RequestMapping("/plan_save.do")
	@ResponseBody
	public JSONObject savePlan(@RequestParam("DeptCode")String  DeptCode,@RequestParam("name") String name,@RequestParam("year") String year,@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime,@RequestParam("practiceObject") String practiceObject,@RequestParam("practicePurpose") String practicePurpose ,@RequestParam("practiceContent") String practiceContent ,@RequestParam("practiceMode") String practiceMode ,@RequestParam("evaluationMethod") String evaluationMethod  ) {
		JSONObject json = new JSONObject();
		try {
			Plan plan = new Plan();
			plan.setDeptCode(DeptCode);
			plan.setName(name);
			plan.setPracticeContent(practiceContent);
			plan.setEvaluationMethod(evaluationMethod);
			plan.setPracticeMode(practiceMode);
			plan.setPracticeObject(practiceObject);
			plan.setPracticePurpose(practicePurpose);
			plan.setEndTime(endTime);
			plan.setYear(year);
			plan.setStartTime(startTime);
			int rel = planService.addPlan(plan);
			json.put("result", rel>0?"success":"error");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return json;
	}
	/**?
	 * 实习计划详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/plan_check.do")
	public ModelAndView planCheck(@RequestParam("id")int  id) {
		ModelAndView mv = new ModelAndView("plan_check");
		Plan plan = planService.getPlanById(id);
		mv.addObject("plan",plan);
		return mv;
	}
	/**?
	 * 实习计划修改
	 * @param id
	 * @return
	 */
	@RequestMapping("/plan_edit.do")
	public ModelAndView planEdit(@RequestParam("id")int  id) {
		this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
		ModelAndView mv = new ModelAndView("plan_edit");
		Plan plan = planService.getPlanById(id);
		mv.addObject("plan",plan);
		mv.addObject("departmentInfoList",departmentInfoList);
		return mv;
	}
	@RequestMapping("/plan_update.do")
	@ResponseBody
	public JSONObject updatePlan(@RequestParam("id")int  id,@RequestParam("DeptCode")String  DeptCode,@RequestParam("name") String name,@RequestParam("year") String year,@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime,@RequestParam("practiceObject") String practiceObject,@RequestParam("practicePurpose") String practicePurpose ,@RequestParam("practiceContent") String practiceContent ,@RequestParam("practiceMode") String practiceMode ,@RequestParam("evaluationMethod") String evaluationMethod  ) {
		JSONObject json = new JSONObject();
		try {
			Plan plan = planService.getPlanById(id);
			plan.setDeptCode(DeptCode);
			plan.setName(name);
			plan.setPracticeContent(practiceContent);
			plan.setEvaluationMethod(evaluationMethod);
			plan.setPracticeMode(practiceMode);
			plan.setPracticeObject(practiceObject);
			plan.setPracticePurpose(practicePurpose);
			plan.setEndTime(endTime);
			plan.setYear(year);
			plan.setStartTime(startTime);
			int rel = planService.updatePlan(plan);
			json.put("result", rel>0?"success":"error");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return json;
	}
	/**?
	 * 删除
	 * @param Id
	 * @return
	 */
		@RequestMapping("/delete.do")
		@ResponseBody
		public JSONObject delPlan(@RequestParam int id){
			JSONObject json = new JSONObject();
			int  rel= planService.delPlan(id);
			json.put("result", rel > 0 ?"success":"error");
			 return json; 
		}
	
}